<?php
namespace Admin\Api;
use Common\Api\Api;

/**
 * Created by PhpStorm.
 * User: an
 * Date: 2015/9/15
 * Time: 15:05
 */
class NewmemberApi extends Api{
    const ADD="Admin/Newmember/add";

    const GET_INFO = "Admin/Newmember/getinfo";

    const QUERY_NO_PAGING = "Admin/Newmember/queryNoPaging";

    const SAVE = "Admin/Newmember/save";

    const QUERY = "Admin/Newmember/query";
    /*
     * 联合查询
     * */
    const UNION_WXUSER_QUERY = 'Admin/Newmember/unionWxuserQuery';

    protected function _init(){
        $this->model= M('new_member');
    }

    /*
     * 联合微信用户表查询
     * */
    public function unionWxuserQuery($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){
            $query = $this->model->join("itboye_wxuser on itboye_new_member.wx_id = itboye_wxuser.id","left");
            if(!is_null($map)){
                $query = $query->where($map);
            }
            if(!($order === false)){
                $query = $query->order($order);
            }
            if(!($fields === false)){
                $query = $query->field($fields);
            }
            $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


            if ($list === false) {
                $error = $this -> model -> getDbError();
                return $this -> apiReturnErr($error);
            }

            $count = $this -> model -> where($map) -> count();
            // 查询满足要求的总记录数
            $Page = new \Think\Page($count, $page['size']);

            //分页跳转的时候保证查询条件
            if ($params !== false) {
                foreach ($params as $key => $val) {
                    $Page -> parameter[$key] = urlencode($val);
                }
            }

            // 实例化分页类 传入总记录数和每页显示的记录数
            $show = $Page -> show();

            return $this -> apiReturnSuc(array("show" => $show, "list" => $list));

    }

}